#!/bin/bash
# whisper decryption skript

# shorcuts
pactl=$(which pactl)
pacmd=$(which pacmd)
paload="$pactl load-module"
paunload="$pactl unload-module"
nullsink="whisperdec"


# DEBUG
# set -x

WHISPERDIR="$(dirname $(dirname $0))"
BINDIR="$WHISPERDIR/io/build-default"
CONFDIR="$WHISPERDIR/config"
crypt="f"
key="0x0001020A0F1020A0F0FF"
port="23122"


# get current sinks
DEFAULT_SINK=$($pacmd list 2>/dev/null | grep "Default sink name:" | awk '{ print $NF }')
DEFAULT_SOURCE=$($pacmd list 2>/dev/null | grep "Default source name:" | awk '{ print $NF }')
PLAYBACK_SINK="${DEFAULT_SINK}"
CAPTURE_SOURCE="${nullsink}.monitor"



function unload_module(){
	name="$1"
	moduleidx=$(pactl list-modules | grep -B1 "$name" | grep index | head -1 | awk '{ print $NF }')
	if [[ -z "$moduleidx" ]]; then
		echo "could not find module $name to unload"
	else
		$paunload $moduleidx
	fi
}

while [[ $# -ge 1 && "${1:0:1}" ]]; 
do
	
	if [[ $# -ge 2 ]]; then
	
		if [[ "$1" == "-c" ]]; then
			crypt="$2"
		fi
		if [[ "$1" == "-k" ]]; then
			key="$2"
		fi
		if [[ "$1" == "-q" ]]; then
			iptqueue="$2"
		fi
		if [[ "$1" == "-p" ]]; then
			port="$2"
		fi
		if [[ "$1" == "-o" ]]; then
			PLAYBACK_SINK="$2"
		fi
		if [[ "$1" == "-i" ]]; then
			CAPTURE_SOURCE="$2"
		fi

		shift
	fi
	shift

done

#if [[ $(id -u) -ne 0 ]]; then
#	echo "You are not root. Make sure sudo iptables is possible."
#	iptcmd="sudo iptables"
#fi



echo "starting pulseaudio decryption"



# DEBUGGING: hear what we say
# PLAYBACK_SINK="$DEFAULT_SINK"


echo "loading null sink"

# load nullsink as sink target (master sink)
null_idx=$($paload module-null-sink sink_name=$nullsink)

#echo "loading rtp receiver"
#
## load rtp receiver
#$paload module-rtp-recv
#
#echo "loading rtp sender"
## send RTP stream
## TODO do not use multicast
#$paload module-rtp-send loop=1 source=${DEFAULT_SOURCE} port=$port
#
#echo "Waiting for sink to create"
#sleep 5
#
## get correct sink input
#sinkinput=$($pactl list-sink-inputs | grep -B1 module-rtp-recv | grep index | head -1 | awk '{ print $NF }')
#
#
#if [[ -z "$sinkinput" ]]; then
#	echo "could not find the correct sink input"
#	unload_module module-rtp-send
#	unload_module module-rtp-recv
#	unload_module module-null-sink
#	exit 2
#fi
#
#echo "moveing sink input"
#$pactl move-sink-input $sinkinput $nullsink

echo "setting default sink"
$pacmd set-default-sink ${nullsink}

#$iptcmd -A OUTPUT -o eth0 -p udp --dport $port -j NFQUEUE --queue-num $iptqueue
#if [[ $? -ne 0 ]]; then
#	echo "iptables command failed"
#	exit 2
#fi
#
#$iptcmd -A INPUT -i eth0 -p udp --dport $port -j NFQUEUE --queue-num $iptqueue
#if [[ $? -ne 0 ]]; then
#	echo "iptables command failed"
#	exit 2
#fi

echo
echo "##################################################################"
echo "# starting stegit-2 framework, press Ctrl+C to quit "
echo "##################################################################"
echo

$BINDIR/stegit-2 -c $CONFDIR/stegit-2.conf -E "$crypt" -R p -W p -o "CRYPTO_ENGINE.IV=${key}" -e "PULSE_IO::PLAYBACK_SINK=${PLAYBACK_SINK}" -e "PULSE_IO::CAPTURE_SOURCE=${CAPTURE_SOURCE}" $@


#$iptcmd -F

#echo "Waiting for input"
#echo "press Enter to quit"
#read

$pacmd set-default-source $DEFAULT_SOURCE
#unload_module module-rtp-send
#unload_module module-rtp-recv
$paunload $null_idx

echo "decryption finished"
